// 该代码在主程序中嵌套创建了子进程和孙进程，然后子进程退出成为僵尸进程，而孙进程休眠2秒后再退出
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main(){
	pid_t pid;

	pid = fork();

	if(pid < 0){
		printf("子进程创建失败!\n");
		return -1;
	}else if(pid == 0){
		printf("02：此时为子进程，进程号为：%d\n", getpid());
		// 创建孙进程
		pid = fork();
		if(pid < 0){
			printf("孙进程创建失败!\n");
			return -2;
		}else if(pid == 0){
			printf("04:此时为孙进程,进程号为：%d\n", getpid());
			sleep(2);
			exit(0);
		}else{
			sleep(2);
			printf("03:此时为孙进程的父进程，即子进程, 进程号为:%d\n", getpid());
			exit(0);
		}

		//exit(0);

	}else{
		printf("01:此时为父进程，进程号为:%d\n",getpid());
		// 此时为父进程，直接退出即可
		exit(0);

	}
	printf("111\n");
	// 判断那个进程退出了
	if(waitpid(pid, NULL, 0) != pid){
		perror("waitpid销毁进程失败!\n");
		exit(0);
	}else{
		printf("子进程%d销毁!\n", getpid());
	}

	exit(0);
}
